package com.zr.shortlink.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zr.shortlink.admin.common.biz.uesr.UserContext;
import com.zr.shortlink.admin.dao.entity.GroupDO;
import com.zr.shortlink.admin.dao.mapper.GroupMapper;
import com.zr.shortlink.admin.dto.req.ShortLinkGroupSortReqDTO;
import com.zr.shortlink.admin.dto.req.ShortLinkGroupUpdateReqDTO;
import com.zr.shortlink.admin.dto.resp.ShortLinkGroupRespDTO;
import com.zr.shortlink.admin.service.GroupService;
import com.zr.shortlink.admin.toolkit.RandomGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 短链接分组接口实现层
 */
@Service
@Slf4j
public class GroupServiceImpl  extends ServiceImpl<GroupMapper, GroupDO> implements GroupService {
    /**
     * 保证gid是唯一的
     */
    String gid;

    public void saveGroup(String groupName) {


        do {
            gid = RandomGenerator.generateCode();
        } while (hasGid(gid));
        GroupDO groupDO = GroupDO.builder()
                .gid(gid)
                .name(groupName)
                .username(UserContext.getUsername())
                .build();

        baseMapper.insert(groupDO);
    }

    @Override
    public List<ShortLinkGroupRespDTO> listGroup() {
        System.out.println("listGroup"+UserContext.getUsername());
        LambdaQueryWrapper<GroupDO> queryWrapper = Wrappers.lambdaQuery(GroupDO.class)
                .eq(GroupDO::getDelFlag, 0)
                .eq(GroupDO::getUsername, UserContext.getUsername())
                .orderByDesc(GroupDO::getSortOrder,GroupDO::getUpdateTime);
        List<GroupDO> groupDOList = baseMapper.selectList(queryWrapper);

        return BeanUtil.copyToList(groupDOList, ShortLinkGroupRespDTO.class);
    }

    @Override
    public void UpdateGroup(ShortLinkGroupUpdateReqDTO requestparam) {
        LambdaQueryWrapper<GroupDO> wrapper = Wrappers.lambdaQuery(GroupDO.class)
                .eq(GroupDO::getUsername, UserContext.getUsername())
                .eq(GroupDO::getGid, requestparam.getGid())
                .eq(GroupDO::getDelFlag, 0);

        GroupDO groupDO = new GroupDO();
        groupDO.setName(requestparam.getName());
        baseMapper.update(groupDO, wrapper);

    }

    @Override
    public void Delete(String gid) {
        LambdaQueryWrapper<GroupDO> wrapper = Wrappers.lambdaQuery(GroupDO.class)
                .eq(GroupDO::getUsername, UserContext.getUsername())
                .eq(GroupDO::getGid, gid)
                .eq(GroupDO::getDelFlag, 0);

        GroupDO groupDO = new GroupDO();
        groupDO.setDelFlag(1);
        int update = baseMapper.update(groupDO, wrapper);
        System.out.println("update"+update);
    }

    @Override
    public void SortOrder(List<ShortLinkGroupSortReqDTO> requestparam) {
        requestparam.forEach(item -> {
            GroupDO groupDO = GroupDO.builder()
                    .sortOrder(item.getSortOrder())
                    .build();
            log.info("sortOrder"+UserContext.getUsername());
            LambdaQueryWrapper<GroupDO> wrapper = Wrappers.lambdaQuery(GroupDO.class)
                    .eq(GroupDO::getUsername, UserContext.getUsername())
                    .eq(GroupDO::getGid, item.getGid())
                    .eq(GroupDO::getDelFlag, 0);
            baseMapper.update(groupDO, wrapper);

        });
    }

    private boolean hasGid(String gid) {
        LambdaQueryWrapper<GroupDO> queryWrapper = Wrappers.lambdaQuery(GroupDO.class)
                .eq(GroupDO::getDelFlag, 0)
                .eq(GroupDO::getName, UserContext.getUsername())
                .eq(GroupDO::getGid, gid);
        GroupDO hasgroupflag = baseMapper.selectOne(queryWrapper);
        return hasgroupflag != null;
    }

}
